home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / dxcmds34.sit / Dartmouth XCMD's 3.4.3 / card_8751.txt < prev    next >
Text File  |  1990-04-17  |  14KB  |  428 lines

  1. -- card: 8751 from stack: in.3
  2. -- bmap block id: 14237
  3. -- flags: 4000
  4. -- background id: 8327
  5. -- name: C Utilities
  6.  
  7.  
  8. -- part 5 (field)
  9. -- low flags: 01
  10. -- high flags: 2007
  11. -- rect: left=18 top=32 right=290 bottom=486
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 0
  15. -- font id: 3
  16. -- text size: 10
  17. -- style flags: 0
  18. -- line height: 13
  19. -- part name: Documentation
  20.  
  21.  
  22. -- part 6 (button)
  23. -- low flags: 00
  24. -- high flags: 8003
  25. -- rect: left=299 top=298 right=320 bottom=438
  26. -- title width / last selected line: 0
  27. -- icon id / first selected line: 0 / 0
  28. -- text alignment: 1
  29. -- font id: 0
  30. -- text size: 12
  31. -- style flags: 0
  32. -- line height: 16
  33. -- part name: Show C Source
  34. ----- HyperTalk script -----
  35. on mouseUp
  36.   get the visible of card field "source"
  37.   set the visible of card field "source" to not it
  38.   if it is false then
  39.     set the name of me to "Hide C Source"
  40.   else
  41.     set the name of me to "Show C Source"
  42.   end if
  43. end mouseUp
  44.  
  45.  
  46.  
  47. -- part 7 (field)
  48. -- low flags: 81
  49. -- high flags: 0007
  50. -- rect: left=18 top=31 right=291 bottom=486
  51. -- title width / last selected line: 0
  52. -- icon id / first selected line: 0 / 0
  53. -- text alignment: 0
  54. -- font id: 3
  55. -- text size: 10
  56. -- style flags: 0
  57. -- line height: 13
  58. -- part name: Source
  59.  
  60.  
  61. -- part 11 (field)
  62. -- low flags: 81
  63. -- high flags: 0007
  64. -- rect: left=18 top=32 right=285 bottom=481
  65. -- title width / last selected line: 0
  66. -- icon id / first selected line: 0 / 0
  67. -- text alignment: 0
  68. -- font id: 3
  69. -- text size: 12
  70. -- style flags: 0
  71. -- line height: 16
  72. -- part name: got
  73.  
  74.  
  75. -- part contents for card part 5
  76. ----- text -----
  77. C Utilities 1.0d4
  78. Roger Brown
  79.  
  80. This is a collection of various C functions that are useful when creating XFCN's.
  81. They are written in THINK CΓäó.
  82.  
  83. New this version:
  84.  
  85. ΓÇó GetCardRect: gets the screen rectangle of the card window.  Works with
  86.    SuperCard.
  87.  
  88. ΓÇó ShowString: a debugging tool that lets you display a string in an answer dialog.
  89.  
  90. ΓÇó GetHCWord: gets a specified word from a HyperCard container.
  91.  
  92. Previously released:
  93.  
  94. ΓÇó Number of HyperCard Items : int NumHCitems(char *source)
  95.    Given a pointer to a source of HyperCard formatted items (comma delimited),  return 
  96.    the number of items in it.
  97.  
  98. ΓÇó Get HyperCard Item  : GetHCItem(char *inStr,  int i,  char *outStr)
  99.    Given a HyperCard item list (comma delimited) in string inStr, get item i and return it
  100.    as a string in outStr.
  101.  
  102. ΓÇó Number of HyperCard Lines : int NumHCLines(char *source)
  103.    Given a pointer to a source of HyperCard formatted lines, such as a field, return the     
  104.    number of lines in it.
  105.  
  106. ΓÇó Get HyperCard Line : GetHCLine(int line,  char *source,  char *dest)
  107.    Given a pointer to a source of lines (like a field), extract the requested line and return 
  108.    it in string dest.
  109.  
  110. ΓÇó Get HyperCard Expression : GetHCExpression(char *inStr,  char *outStr)
  111.    Given a HyperCard expression in inString, return its value in outStr.
  112.  
  113. ΓÇó String Contains : StrContains(char *target,  char *test)
  114.    Given string test, return TRUE if it contains the string target.
  115.  
  116. ΓÇó Get HyperCard  Card WindowRectangle : GetCardRect(Rect *itsRect)
  117.    Get the rectangle of HyperCard's card window.
  118.  
  119. ΓÇó Build a return result : ResultIs(XCmdBlockPtr paramPtr,  char *theResult)
  120.    Given a C formatted string, build a return result structure for the given parameter 
  121.    block.
  122.  
  123. ΓÇó GetHCRectangle: GetHCRectangle(str,aRect)
  124.    Given a HyperCard rectangle specification in a string, return a QuickDraw rectangle.
  125.  
  126.  
  127. -- part contents for card part 7
  128. ----- text -----
  129. /* C Utilities 1.0a4.c */
  130. /* version 1.0a4 9/14/89 */
  131. /* Roger Brown, Dartmouth Courseware Development Group 7/7/88 */
  132. /* HyperCard XCMD support library */
  133.  
  134. #include "HyperXCmd.h"
  135.  
  136. /* change a string to all upper case */
  137.  
  138. ucase(s)
  139. char *s;
  140. {
  141.   int i;
  142.   char c;
  143.   
  144.   for (i=0;i<strlen(s);i++) {
  145.     s[i] = toupper(s[i]);
  146.   }
  147. }
  148.  
  149. /* get a specified word from a HyperCard container */
  150.  
  151. GetHCWord(inStr,i,outStr)
  152. char *inStr,*outStr;
  153. int i;
  154. {
  155.   int c;                                           /* character pointer */
  156.   int len;                                         /* length to scan */
  157.   int count;                                       /* count of items found */
  158.   int j;                                           /* item byte count */
  159.   Str255 temp;                                     /* collect item here, hope its < 255 */
  160.   
  161.   count = j = 0;                                   /* initialize */
  162.   len = strlen(inStr);                             /* go this far, max */
  163.   for (c=0;c<len;c++) {
  164.     if (inStr[c]==' ') {                         /* at an word boundary */
  165.       count = count + 1;                       /* bump counter */
  166.       if (count==i) break;                     /* if this is it, stop scanning */
  167.       j = 0;                                   /* else start on the next item */
  168.     }
  169.     else {
  170.       temp[j] = inStr[c];                      /* collect characters to return */
  171.       j++;                                     /* go to next */
  172.       if (j==255) {                            /* sorry, too big to store */
  173.         strcpy(temp,"Error: word > 255 characters.");
  174.         return;                       
  175.       }
  176.       if (c==(len-1)) {                        /* last word, no space */
  177.         count = count+1;
  178.         break;
  179.       }
  180.     }
  181.   }
  182.   if (count < i) strcpy(outStr,"Error: word out of range");   /* no word there */
  183.   else {
  184.     temp[j] = 0;                                     /* make it a C string */
  185.     strcpy(outStr,temp);                             /* move to output string */
  186.   }
  187.   return;
  188. }
  189.  
  190. /* Get the number of HyperCard comma delimited items in string s. */
  191.  
  192. int NumHCItems(s)
  193. char *s;
  194. {
  195.   int c;                                           /* character pointer */
  196.   int len;                                         /* length of string */
  197.   int count;                                       /* count of items found */
  198.   int j;                                           /* item byte counter */  
  199.   
  200.   count = j = 0;                               /* initialize */
  201.   len = strlen(s);                                 /* will look this far */
  202.   for (c=0;c<len;c++) {                            /* scan all characters */
  203.     if (s[c]==',') {                             /* found end of an item */
  204.       count = count + 1;                       /* bump item found counter */
  205.       j = 0;                                   /* reset item scan counter */
  206.     }
  207.     else {                                       
  208.       j++;                                     /* bump item byte counter */
  209.       if (c==(len-1)) {                        /* last item, no comma */
  210.         count = count+1;                     /* this one counts, too */
  211.         break;
  212.       }
  213.     }
  214.   }
  215.   return count;
  216. }
  217.  
  218. /* Get HyperCard comma delimited item i from item list string inStr. Return it in outStr */
  219. /* item must be smaller than 255 characters */
  220.  
  221. GetHCItem(inStr,i,outStr)
  222. char *inStr,*outStr;
  223. int i;
  224. {
  225.   int c;                                           /* character pointer */
  226.   int len;                                         /* length to scan */
  227.   int count;                                       /* count of items found */
  228.   int j;                                           /* item byte count */
  229.   Str255 temp;                                     /* collect item here, hope its < 255 */
  230.   
  231.   count = j = 0;                                   /* initialize */
  232.   len = strlen(inStr);                             /* go this far, max */
  233.   for (c=0;c<len;c++) {
  234.     if (inStr[c]==',') {                         /* at an item boundary */
  235.       count = count + 1;                       /* bump counter */
  236.       if (count==i) break;                     /* if this is it, stop scanning */
  237.       j = 0;                                   /* else start on the next item */
  238.     }
  239.     else {
  240.       temp[j] = inStr[c];                      /* collect characters to return */
  241.       j++;                                     /* go to next */
  242.       if (j==255) {                            /* sorry, too big to store */
  243.         strcpy(temp,"Error: Item > 255 characters.");
  244.         return;                       
  245.       }
  246.       if (c==(len-1)) {                        /* last item, no comma */
  247.         count = count+1;
  248.         break;
  249.       }
  250.     }
  251.   }
  252.   if (count < i) strcpy(outStr,"Error: item out of range");   /* no item there */
  253.   else {
  254.     temp[j] = 0;                                     /* make it a C string */
  255.     strcpy(outStr,temp);                             /* move to output string */
  256.   }
  257.   return;
  258. }
  259.  
  260. /* how many HyperCard lines in string source  */
  261.  
  262. NumHCLines(source) 
  263. char *source;
  264. {
  265.   int c;                                           /* character counter */
  266.   int l;                                           /* line counter */
  267.   int len;
  268.   
  269.   len = strlen(source);
  270.   l = 0;
  271.   for (c=0;c<len;c++) {                            /* scan to desired line */
  272.     if (source[c]==13) l++;
  273.   }
  274.   if (source[c-1]!=13) l++;                        /* last line might not have CR */
  275.   return l;
  276.   
  277. }
  278. /* get HyperCard line from source and return in dest */
  279.  
  280. GetHCLine(line,source,dest)
  281. int line;
  282. char *source,*dest;
  283. {
  284.   int i;                                                /* char counter before line */
  285.   int j;                                                /* char cou,tner after line */
  286.   int c;                                                /* line counter */
  287.   int len;                                              /* length of source */
  288.       
  289.   len = strlen(source);                                 /* go this far, max */
  290.   c = 1;                                                /* initialize */
  291.   i = 0;
  292.   while (c<line) {                                      /* cycle to desired line */
  293.     if (source[i]==13) c++;                           /* bump line count */
  294.     i++;
  295.     if (i>len) {                                      /* out of range */
  296.       strcpy(dest,"Error: Line out of range."); 
  297.       return;
  298.     }
  299.   }
  300.   c = 0;                                                /* at line, start transfer */
  301.   for (j=i;j<len;j++) {
  302.     *(dest+c) = source[j];
  303.     if (source[j]==13) break;                         /* line ended */
  304.     c++;
  305.   }
  306.   *(dest+c) = (char)0;                                  /* make into a C string */
  307. }
  308.  
  309. /* Given a HyperCard expression in inStr, return its value in outStr */
  310.  
  311. GetHCExpression(paramPtr,inStr,outStr)
  312. XCmdBlockPtr  paramPtr;
  313. char *inStr;
  314. Str255 *outStr;
  315. {
  316.   Handle theResult;                                     /* handle to the final result */
  317.   long len;                                             /* length needed */
  318.   #ifdef XCMD
  319.   strcpy(outStr,inStr);                    /* cheat a little, use outStr as an intermediate */
  320.   
  321.   CtoPstr((char *)outStr);                                      /* make it a P string */
  322.   theResult = EvalExpr(paramPtr,outStr);               /* get its value from HC */
  323.   len = GetHandleSize(theResult);                       /* how long is it? */
  324.   BlockMove(*theResult,outStr,len);                     /* move the result into outStr */
  325.   DisposHandle(theResult);                              /* tidy up */
  326.   #endif
  327. }
  328.  
  329. /* Return true if target string contains test string. Match is not case sensitive. */
  330.  
  331. char StrContains(target,test) 
  332. char *target,*test;
  333. {
  334.   char ok;
  335.   int i;
  336.   char *j,*k;
  337.   int targetLen,testLen;
  338.   char go;
  339.   
  340.   targetLen = strlen(target);                            /* scan this far, max */
  341.   testLen = strlen(test);                                /* look for this many matches */
  342.   for (i=0;i<targetLen;i++) { 
  343.     j = target+i;
  344.     k = test;
  345.     while (toupper(*j)==toupper(*k)) {                 /* loop while they match */
  346.       j++;
  347.       k++;
  348.       if (k==(test+testLen)) return TRUE;            /* got it */
  349.     }
  350.   }
  351.   return FALSE;                                          /* no match */
  352. }
  353.  
  354. /* Get the coordiates of the HyperCard card window */
  355.  
  356. GetCardRect(itsRect)
  357. Rect *itsRect;
  358. {
  359.   /* This version will work with SuperCard */
  360.   
  361.   WindowPtr cardWindow;
  362.   Rect r;
  363.   Point thePt;
  364.     
  365.     GetPort(&cardWindow);
  366.     r = (*cardWindow).portRect;
  367.     SetPt(&thePt,0,0);
  368.     LocalToGlobal(&thePt);
  369.     OffsetRect(&r,thePt.h,thePt.v);
  370.     *itsRect = r;
  371. }
  372.  
  373. /* build a return result structure from a string */
  374.  
  375. ResultIs(paramPtr,theResult)
  376. XCmdBlockPtr  paramPtr;
  377. char *theResult;
  378. {
  379.   long len;
  380.   Handle resultHandle;
  381.   len = 1+strlen(theResult);
  382.   resultHandle = NewHandle(len);
  383.   BlockMove(theResult,*resultHandle,len);
  384.   paramPtr->returnValue = resultHandle;
  385. }
  386.  
  387. /* show a string in an answer dialog - for debugging */
  388.  
  389. ShowString(paramPtr,text)
  390. XCmdBlockPtr paramPtr;
  391. char *text;
  392. {
  393.     Str255 temp;
  394.     strcpy(temp,"answer \" ");
  395.     strcat(temp,text);
  396.     strcat(temp,"\"");
  397.     CtoPstr((char *)temp);
  398.     SendCardMessage(paramPtr,temp);
  399. }
  400.  
  401. /* Change a HyperCard rectangle description into a QuickDraw rectangle */
  402.  
  403. GetHCRect(str,itsRect)
  404. Str255  str;
  405. Rect *itsRect;
  406. {
  407.         Str255 it;
  408.         long corner;
  409.         
  410.         GetHCItem(str,1,it);
  411.         CtoPstr((char *)it);
  412.         StringToNum(it,&corner);
  413.         itsRect->left = (int)corner;
  414.         GetHCItem(str,2,it);
  415.         CtoPstr((char *)it);
  416.         StringToNum(it,&corner);
  417.         itsRect->top = (int)corner;
  418.         GetHCItem(str,3,it);
  419.         CtoPstr((char *)it);
  420.         StringToNum(it,&corner);
  421.         itsRect->right = (int)corner;
  422.         GetHCItem(str,4,it);
  423.         CtoPstr((char *)it);
  424.         StringToNum(it,&corner);
  425.         itsRect->bottom = (int)corner;
  426. }
  427.  
  428.